function [taub h sig Z S sigma sen] = MKTest(datain, alpha)
    sorted = sortrows(datain,1);

    row1 = sorted(:,1)';
    row2 = sorted(:,2)';
    
    clear sorted;

    L1 = length(row1);
    L2 = L1 - 1;

    ro1 = sort(row1)';
    ro2 = sort(row2)';
    [a,b] = unique(ro1);
    [d,e] = unique(ro2);
   
    clear a c d f ro1 ro2;

    if b(1,1) > 1 
        ta = b(1,1);
    else
        ta = 1;
    end
    if e(1,1) > 1 
        tb = e(1,1);
    else
        tb = 1;
    end
    bdiff = [ta; diff(b)];
    ediff = [tb; diff(e)];
    
    clear ta tb b e;
    
    t1 = sum(bdiff .* (bdiff - 1) .* (2 .* bdiff + 5));
    t2 = sum(ediff .* (ediff - 1) .* (2 .* ediff + 5));
    ties = t1 + t2;

    t1a = (sum(bdiff .* (bdiff - 1))) / 2;
    t2a = (sum(ediff .* (ediff - 1))) / 2;
    
    m1 = repmat((1:L2)',[1 L2]);
    m2 = repmat((2:L1)',[1 L2])';

    A1 = triu(row1(m1));
    A2 = triu(row1(m2));
    B1 = triu(row2(m1));
    B2 = triu(row2(m2));
    
    clear m1 m2 row1 row2;
    
    A = sign(A1 - A2);
    B = sign(B1 - B2);
    
    %perform operations to calculate Sen's Slope 
    A3 = reshape((A1 - A2),L2*L2,1);
    B3 = reshape((B1 - B2),L2*L2,1);
    a = find(A3~=0);
    C3 = B3(a)./A3(a);
    sen = median(C3);
    
    clear A1 A2 B1 B2 A3 B3 C3 a;
  
    % evaluate concordant and discordant
    % +1 = concordant
    % -1 = discordant
    %  0 = tie
    C = A.*B;
    % Compute S
    S = sum(sum(C,2));
    
    clear A B C;

    % Calculate denominator with ties removed
    D = sqrt(((.5*L1*(L1-1))-t1a)*((.5*L1*(L1-1))-t2a));
    
    % adjust for continuity for small n
    if L1 <= 10
        %
        if S > 0
            S = S -1;
        elseif S < 0
            S = S + 1;
        end
    end
    
    taub = S / D;
    
    sigma = sqrt(((L1*(L1-1)*(2*L1 + 5)) - ties)/18);
   
    Z = S / sigma;

    % h = 1 : means significance
    % h = 0 : means not significant (i.e. sig < z(sig))
    [h, sig] = ztest(S,0,sigma,alpha);
   
end


